package com.oig.sys.base.controller;

import cn.hutool.json.JSONUtil;
import com.oig.common.module.SysResponse;
import com.oig.common.validator.GroupAdd;
import com.oig.common.validator.GroupEdit;
import com.oig.sys.base.app.IAppHsGoodsTariffService;
import com.oig.sys.base.dto.HsGoodsTariffDto;
import com.oig.sys.base.vo.HsGoodsTariffVo;
import com.oig.sys.log.annotation.SysLog;
import com.oig.sys.security.util.SecurityUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "进口税率维护")
@RestController
@RequestMapping("/hsGoodsTariff")
@Slf4j
@RequiredArgsConstructor
public class TbHsGoodsTariffController {

    private final IAppHsGoodsTariffService appHsGoodsTariffService;


    @PreAuthorize("@pms.hasPermission('/hsGoodsTariff/addCountryGroup')")
    @SysLog(value="根据国别组添加协定进口关税税率",apiUrl="/hsGoodsTariff/addCountryGroup")
    @Operation(summary = "根据国别组添加协定进口关税税率")
    @PostMapping("/addCountryGroup")
    public List<HsGoodsTariffVo> addCountryGroup(@RequestBody @Validated(GroupAdd.class) HsGoodsTariffDto hsGoodsTariffDto){
        log.info("add CountryGroup hsGoodsTariff:{}", hsGoodsTariffDto);
        hsGoodsTariffDto.setLastUpdateId(SecurityUtil.getDefineUserMain().getId());
        hsGoodsTariffDto.setLastUpdateName(SecurityUtil.getDefineUserMain().getRealName());
        return this.appHsGoodsTariffService.addGroupHsGoodsTariff(hsGoodsTariffDto);
    }


    @PreAuthorize("@pms.hasPermission('/hsGoodsTariff/add')")
    @SysLog(value="添加指定国家进口关税税率",apiUrl="/hsGoodsTariff/add")
    @Operation(summary = "添加指定国家进口关税税率")
    @PostMapping("/add")
    public HsGoodsTariffVo add(@RequestBody @Validated(GroupAdd.class) HsGoodsTariffDto hsGoodsTariffDto){
        log.info("add hsGoodsTariff:{}", hsGoodsTariffDto);
        hsGoodsTariffDto.setCreateUserId(SecurityUtil.getDefineUserMain().getId());
        hsGoodsTariffDto.setCreateUserName(SecurityUtil.getDefineUserMain().getRealName());
        return this.appHsGoodsTariffService.addHsGoodsTariff(hsGoodsTariffDto);
    }

    @PreAuthorize("@pms.hasPermission('/hsGoodsTariff/update')")
    @SysLog(value="修改指定国家进口关税税率",apiUrl="/hsGoodsTariff/update")
    @Operation(summary = "修改指定国家进口关税税率")
    @PostMapping("/update")
    public HsGoodsTariffVo update(@RequestBody @Validated(GroupEdit.class) HsGoodsTariffDto hsGoodsTariffDto){
        log.info("update goods :{}", hsGoodsTariffDto);
        hsGoodsTariffDto.setLastUpdateId(SecurityUtil.getDefineUserMain().getId());
        hsGoodsTariffDto.setLastUpdateName(SecurityUtil.getDefineUserMain().getRealName());
        return this.appHsGoodsTariffService.updateHsGoodsTariff(hsGoodsTariffDto);
    }

    @PreAuthorize("@pms.hasPermission('/hsGoodsTariff/del')")
    @SysLog(value="注销指定国家进口关税税率",apiUrl="/hsGoodsTariff/del")
    @Operation(summary = "注销指定国家进口关税税率")
    @PostMapping("/del/{id}")
    public SysResponse<Integer> del(@PathVariable("id")Integer id){
        log.info("cancel goods:{}", id);
        this.appHsGoodsTariffService.delHsGoodsTariff(id);
        return SysResponse.ok(id);
    }

    @Operation(summary = "根据国家获取指定hscode进口税率")
    @PostMapping("/getTariff")
    public HsGoodsTariffVo getTariff(@RequestParam("countryCode")String countryCode, @RequestParam("hsCode") String hsCode){
        log.info("get goods Tariff:{}-{}", countryCode,hsCode);
        return this.appHsGoodsTariffService.getTariff(countryCode,hsCode);
    }


    @Operation(summary = "批量根据国家获取指定hscode进口税率")
    @PostMapping("/getTariffList")
    public List<HsGoodsTariffVo> getTariffByHSCodeAndCountryCodeList(@RequestBody List<HsGoodsTariffDto> req){
        log.info("get goods Tariff: {}", JSONUtil.toJsonStr(req));
        return this.appHsGoodsTariffService.getTariffByHSCodeAndCountryCodeList(req);
    }

    @Operation(summary = "根据hscode获取优惠国家税率列表")
    @PostMapping("/getTariffList/{hsCode}")
    public List<HsGoodsTariffVo> getTariffList(@PathVariable(name="hsCode") String hsCode){
        log.info("get goods all country tariff,hsCode={}",hsCode);
        return this.appHsGoodsTariffService.getHsGoodsAllTariff(hsCode);
    }

    @Operation(summary = "根据id获取优惠税率国家")
    @PostMapping("/getTariffInfo/{id}")
    public HsGoodsTariffVo getHsGoodsTariffInfo(@PathVariable(name="id") Integer id){
        log.info("get getHsGoodsAllTariffInfo,id={}",id);
        return this.appHsGoodsTariffService.getHsGoodsTariffInfo(id);
    }

}
